<template>
  <div>
    <el-dialog title="新增/编辑二级分类" :visible.sync="addTwoCatevis" @close="close" width="500px" append-to-body>
        <el-form :rules="rules" :model="addTwoCateForm" ref="addTwoCateForm">

            <el-form-item label="二级类别编号:" prop="categoryTwoNumber" :label-width="formLabelWidth">
                <el-input v-model="addTwoCateForm.categoryTwoNumber" disabled placeholder="自动生成"></el-input>
            </el-form-item>
            <el-form-item label="二级类别名称:" prop="categoryTwoName" :label-width="formLabelWidth">
                <el-input v-model="addTwoCateForm.categoryTwoName"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="danger" :loading="submitLoading" @click="submitProdCategory('addTwoCateForm')">确 定</el-button>
        </div>
          </el-dialog>
        </div>
</template>

<script>
import { AddProdTwoCate, EditProdTwoCate } from '@/api/prod';
export default {
  name: 'addtwocate',
  props: ['addtwoDialog', 'checkCategory', 'categoryId'],
  data() {
    return {
      addTwoCatevis: false,
      submitLoading: false,
      addTwoCateForm: {
        categoryTwoNumber: '',
        categoryTwoName: '',
      },
      formLabelWidth: '120px',
      rules: {
          categoryTwoName: [
            { required: true, message: '请输入二级分类名称', trigger: 'blur' },
          ],
      }
    }
  },
  methods: {
    close() {
      this.addTwoCatevis = false;
      this.resetForm();
      this.$emit("update:addtwoDialog", false);
    },
    resetForm() {
      this.addTwoCateForm.categoryTwoNumber = '';
      this.addTwoCateForm.categoryTwoName = '';
    },
    submitProdCategory(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {

                if (this.checkCategory) {
                  console.log(this.checkCategory)
                  // 修改类别
                  let reqData = {
                      categoryTwoId: this.checkCategory.categoryTwoId,
                      categoryTwoNumber: this.addTwoCateForm.categoryTwoNumber,
                      categoryTwoName: this.addTwoCateForm.categoryTwoName,
                  }
                  EditProdTwoCate(reqData).then(response => {
                      let data = response.data
                      this.$message({
                          message: data.msg,
                          type: 'success'
                      })
                      this.submitLoading = false;
                      this.$refs.addTwoCateForm.resetFields();
                      this.resetForm();
                      this.addTwoCatevis = false;
                      this.$emit("update:addtwoDialog", false);
                  }).catch(error => {
                      this.submitLoading = false;
                  })

                } else {
                  // 新增二级类别
                  let reqData = {
                      categoryTwoNumber: this.addTwoCateForm.categoryTwoNumber,
                      categoryTwoName: this.addTwoCateForm.categoryTwoName,
                      categoryId: this.categoryId,
                  }
                  this.submitLoading = true;
                  AddProdTwoCate(reqData).then(response => {
                      let data = response.data
                      this.$message({
                          message: data.msg,
                          type: 'success'
                      })
                      this.submitLoading = false;
                      this.$refs.addTwoCateForm.resetFields();
                      this.resetForm();
                      this.addTwoCatevis = false;
                      this.$emit("update:addtwoDialog", false);
                  }).catch(error => {
                      this.submitLoading = false;
                  })
                }
              
            } else {
              console.log('error submit!!');
              return false;
            }
        });
    }
  },
  watch: {
    addtwoDialog(newVal,oldVal) {  
      this.addTwoCatevis = newVal;
    },
    checkCategory(newVal) {
      if (newVal) {
        this.addTwoCateForm.categoryTwoNumber = newVal.categoryTwoNumber;
        this.addTwoCateForm.categoryTwoName = newVal.categoryTwoName;
      }
    },
  }
}
</script>

<style lang="scss" scoped>
  
</style>